package adminDao

import (
	"gitee.com/yangguoliang001/go-gin-vue3/QO"
	"gitee.com/yangguoliang001/go-gin-vue3/model"
	"gitee.com/yangguoliang001/go-gin-vue3/vo"
)

func (a *AdminDao) QueryAdminByUsername(username string) (admin model.AdminDO, err error) {
	err = a.db.Debug().Table("admin").Where("username=?", username).First(&admin).Error
	return
}

func (a *AdminDao) QueryAdminList(adminListReq QO.AdminListReq) (adminListRes vo.AdminListVO, err error) {
	var total int64
	var adminList []model.AdminDO
	err = a.db.Debug().Table("admin").Where("username like ? AND nickname like ? AND mobile like ? AND is_deleted = -1", "%"+adminListReq.Username+"%", "%"+adminListReq.Nickname+"%", "%"+adminListReq.Mobile+"%").Count(&total).Limit(adminListReq.PageSize).Offset((adminListReq.Page - 1) * adminListReq.PageSize).Find(&adminList).Error
	//fmt.Println("adminListRes", adminList[0].CreatedAt, adminList[0].UpdatedAt)

	return vo.AdminListVO{
		Total:     total,
		AdminList: adminList,
		Page:      adminListReq.Page,
		PageSize:  adminListReq.PageSize,
	}, err
}

func (a *AdminDao) AddAdmin(adminReq QO.AdminReq) (affectedRos int64, err error) {
	//
	result := a.db.Debug().Table("admin").FirstOrCreate(&adminReq, "username = ? AND mobile = ?", adminReq.Username, adminReq.Mobile)
	return result.RowsAffected, result.Error
}

func (a *AdminDao) DisabledAdmin(adminReq QO.AdminIdReq) (err error) {
	result := a.db.Debug().Table("admin").Where("id = ?", adminReq.Id).Update("is_used", adminReq.Used)
	return result.Error
}

func (a *AdminDao) DeletedAdmin(id int32) (err error) {
	err = a.db.Debug().Table("admin").Where("id = ?", id).Update("is_deleted", 1).Error
	return
}
